<?php namespace Home\Controller;
 
 class StudyController extends ConmentController{
 	public function index(){
// 		获得当前班级的id
 		$cid=$_GET['cid'];
//		p($cid);
//		关联查询 获得结果
		$data=M()->query("select s.*,c.cname from study as s join class as c on s.cid=c.cid where s.cid=$cid");
//		p($data);
//	分配数据，在表单中显示
		$this->assign("data",$data);
		
 		$this->display();
 	}
	


/*

添加
*/
	public function add(){
//		先获取班级id
		$cid=$_GET['cid'];
		$data= M()->query("select * from class where cid=$cid");
//		p($data);
		$this->assign("data",$data);
		if(IS_POST){
			$cid=$_POST['cid'];
			$name=$_POST['name'];
			$age=$_POST['age'];
			$sex=$_POST['sex'];
			$rows=M()->exec("insert into study (cid,name,age,sex) values('$cid','$name','$age','$sex')");
			if($rows){
				$this->success("添加成功", "index.php?m=Home&c=Study&a=index&cid='$cid'");
			}
		}
		$this->display();
	}
/*
 * 
 *
 编辑 
 * */
 	public function edit(){
 		
 		$id=$_GET['id'];
		
		$data=M()->query("select * from study where id=$id");
//		p($data);
		$this->assign("data",$data);
		if(IS_POST){
			$cid=$_POST['cid'];
			$name=$_POST['name'];
			$age=$_POST['age'];
			$sex=$_POST['sex'];
			$rows=M()->exec("update study set cid='$cid',name='$name',age='$age',sex='$sex' where id='$id'");
			p($rows);
			if($rows){
					$this->success("编辑成功","index.php?m=Home&c=Study&a=index&cid='$cid'");
				}else{
					$this->error('编辑失败');
				}
		}
 		
		$this->display();
 	}
	
/*
 * 删除
 * */
 public function del(){
 		
		$id=$_GET['id'];
		$data=M()->query("select cid from study where id=$id");
		$cid=$data[0]['cid'];
		    try{
		    $sql="delete from study where id=$id";
			$rows=M()->exec($sql);
				if($rows){
					$this->success('删除成功',"index.php?m=Home&c=Study&a=index&cid='$cid'");
				}else{
					$this->error('删除失败');
				}
		       }
		   catch(PDOException $e ){
		       die ($e->getmessage());
		       }
	}
 }
